<?php
namespace App\Http\Controllers\ApiWx;

use App\DBs\FarmDB;
use App\DBs\VideoDB;
use Illuminate\Http\Request;

class VideoController extends BaseController
{
    //视频

    protected $db;
    protected $farm;

    public function __construct()
    {
        parent::__construct();
        $this->db = new VideoDB();
        $this->farm = new FarmDB();
    }

    /**
     * 一个
     */
    public function getOne(Request $request)
    {
        $wx = $request->post('wx','');
        $id = $request->post('id',0);
        $oper = $request->post('oper',0); //1上拉，2下拉，
        if (!in_array($oper,[1,2])) {
            return $this->toWxJson(-2);
        }
        $resDb = $this->db->getOneByOper($wx,$id,$oper,1);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data'];
        return $this->toWxJson(0,$items);
    }

    /**
     * 一个
     */
    public function getOneById(Request $request)
    {
        $vid = $request->post('vid',0);
        $resDb = $this->db->getOneById($vid);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data'];
        return $this->toWxJson(0,$items);
    }

    /**
     * 点赞设置
     */
    public function setZan(Request $request)
    {
        $wx = $request->post('wx','');
        $vid = $request->post('id',0);
        if (!$wx) {
            return $this->toWxJson(-4);
        }
        if (!$vid) {
            return $this->toWxJson(-2);
        }
        $resOne = $this->db->getZanOne($wx,$vid);
        if ($resOne) {
            $resDb = $this->db->setZanDel($wx,$vid);
        } else {
            $resDb = $this->db->setZanAdd($wx,$vid);
        }
        if ($resDb['code']!=0) {
            return $this->toWxJson(-500,$resDb['msg']);
        }
        return $this->toWxJson(0);
    }

    /**
     * 分享设置
     */
    public function setFxAdd(Request $request)
    {
        $wx = $request->post('wx','');
        $vid = $request->post('id',0);
        if (!$wx) {
            return $this->toWxJson(-4);
        }
        if (!$vid) {
            return $this->toWxJson(-2);
        }
        $resDb = $this->db->setFxAdd($wx,$vid);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-500,$resDb['msg']);
        }
        return $this->toWxJson(0);
    }

    /**
     * 码查
     */
    public function getMacha(Request $request)
    {
        $wx = $request->post('wx','');
        $tid = $request->post('tid',0);
        if (!$wx || !$tid) {
            return $this->toWxJson(-2);
        }
        //农果信息
        $resOne = $this->db->getOneByTid($tid);
        $one = $resOne['code']==0 ? $resOne['data'] : array();
        //日常养护
        $resGuo = $this->db->getGuosByTid($tid);
        $guo = $resGuo['code']==0 ? $resGuo['data'] : array();
        //评论
        $resPl = $this->db->getPlsByTid($tid);
        $pl = $resPl['code']==0 ? $resPl['data'] : array();
        //分享
        $resFx = $this->db->getFxsByTid($tid);
        $fx = $resFx['code']==0 ? $resFx['data'] : array();
        $items = [
            'tree'=>$one, 'guos'=>$guo, 'pls'=>$pl, 'fxs'=>$fx,
        ];
        return $this->toWxJson(0,$items);
    }

    /**
     * 领取申请
     */
    public function setFarmApply(Request $request)
    {
        $wx = $request->post('wx','');
        $tid = $request->post('tid',0);
        if (!$wx || !$tid) {
            return $this->toWxJson(-2);
        }
        $resDb = $this->farm->setFarmApply($wx,$tid);
        if ($resDb['code']==-2) {
            return array('code'=>'-5','msg'=>'已领取或者已申请');
        } else if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }
}